/*
 * @(#)MaintWorkflowMonitoringAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.workflow.bean.WorkflowProgress;
import com.dcivision.workflow.bean.WorkflowRecord;
import com.dcivision.workflow.core.WorkflowPermissionManager;
import com.dcivision.workflow.core.WorkflowProgressManager;
import com.dcivision.workflow.dao.MtmWorkflowChildParentProgressDAObject;
import com.dcivision.workflow.dao.WorkflowProgressDAObject;
import com.dcivision.workflow.dao.WorkflowRecordDAObject;

/**
  MaintWorkflowMonitoringAction.java

  This class is for maint workflow tracking .

    @author          Angus Shiu
    @company         DCIVision Limited
    @creation date   29/07/2003
    @version         $Revision: 1.13 $
*/
public class MaintWorkflowMonitoringAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.13 $";

  /** Creates a new instance of MaintWorkflowMonitoringAction */
  public MaintWorkflowMonitoringAction() {
    super();
  }

  /** getFunctionCode
   *
   * Abstract function which sub-class should implement to return the corresponding
   * function code.
   *
   * @return   The function code
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.WORKFLOW_MONITOR);
  }

  /** getMajorDAOClassName
   *
   * Abstract function which sub-class should implement to return the corresponding
   * major DAO class name used in this class.
   *
   * @return   The DAO class name
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.workflow.dao.WorkflowRecordDAObject");
  }

  public void clearUniqueFields(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintWorkflowRecordForm workflowRecordForm = (MaintWorkflowRecordForm) form;
    workflowRecordForm.setID(null);
  }

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    SessionContainer sessionCon = this.getSessionContainer(request);
    Connection dbConn = this.getConnection(request);
    MtmWorkflowChildParentProgressDAObject childParentDAO = (MtmWorkflowChildParentProgressDAObject) this.getDAObjectByClassName(request, "com.dcivision.workflow.dao.MtmWorkflowChildParentProgressDAObject");
    WorkflowProgressManager progressManager = new WorkflowProgressManager(this.getSessionContainer(request), this.getConnection(request));
    WorkflowProgressDAObject progressDAO = new WorkflowProgressDAObject(this.getSessionContainer(request), this.getConnection(request)); 
    MaintWorkflowRecordForm recordForm = (MaintWorkflowRecordForm)form;
    boolean interruptionCompletedWf = SystemParameterFactory.getSystemParameterBoolean(SystemParameterConstant.WORKFLOW_SUPPORT_INTERRUPTION_COMPLETED_WORKFLOW);
    
    Integer trackID = TextUtility.parseIntegerObj(request.getParameter("trackID"));
    Integer workflowRecordID = TextUtility.parseIntegerObj(request.getParameter("ID"));
    List subworkflowList = childParentDAO.getListByParentTrackID(trackID);
    request.setAttribute("subworkflowList", subworkflowList);

    Integer workflowGroupID=null;
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    WorkflowRecordDAObject recordDAO=new WorkflowRecordDAObject(sessionContainer, conn);
    AbstractBaseObject workflowRecordObj=null;
    if(workflowRecordID!=null){
      workflowRecordObj=recordDAO.getObjectByID(workflowRecordID);
      workflowGroupID=((WorkflowRecord)workflowRecordObj).getWorkflowGroupID();
    }
    request.setAttribute("workflowGroupID",workflowGroupID);

    WorkflowPermissionManager workflowPermissionManager = new WorkflowPermissionManager(sessionCon, dbConn);
    List progressList= progressManager.getWorkflowProgressList(trackID);
    List attachmentList = new ArrayList();
    for (int i=0; i<progressList.size(); i++) {
      WorkflowProgress progress = (WorkflowProgress) progressList.get(i);
      boolean hasReadRight = workflowPermissionManager.hasAccessRight(null, progress,
          WorkflowPermissionManager.READ_PERMISSION, sessionCon.getUserRecordID());
      if (hasReadRight) {        
        attachmentList.addAll(progressManager.getWorkflowProgressAttachmentList(progress.getID()));
      }
    }
    request.setAttribute("attachmentList", attachmentList);

    List interruptProgressList = new ArrayList();
    boolean isCompleteInterrupt = false;
    if(interruptionCompletedWf){
      interruptProgressList = progressDAO.getProgressListByTrackIDAndStatus(trackID,null);
      WorkflowProgress tmpworkflowProgress = (WorkflowProgress)interruptProgressList.get(0);
      if(!(GlobalConstant.TRUE).equals(tmpworkflowProgress.getCompleteFlag())){
        interruptProgressList = progressDAO.getNonFinishedProgressListByTrackID (trackID);	
      }else{
        isCompleteInterrupt = true;
      }
    }else{    
      interruptProgressList = progressDAO.getNonFinishedProgressListByTrackID ( trackID );
    }
    if (!Utility.isEmpty(interruptProgressList)) {
      WorkflowProgress workflowProgress = null;
      if(isCompleteInterrupt){
        workflowProgress = (WorkflowProgress)interruptProgressList.get(interruptProgressList.size()-1);
      }else{
        workflowProgress = (WorkflowProgress)interruptProgressList.get(0);
      }
      recordForm.setInterruptAble(progressManager.isInterruptAble(workflowProgress, workflowRecordID));
   }
    return super.execute(mapping, form, request, response);
  }
}
